//
//  EffectController.swift
//  DSP
//
//  Created by tucici on 2024/5/24.
//

import UIKit
import TCCKit
struct CellMode{
    var name = ""
    var data : [Int]
}
class EffectController: DSAlertController {
    private var bgView : UIView!
    private var name : UILabel!
    private var line : UIView!
    private var array = [CellMode]()
    var block : (([Int],Int)->Void)?
    var select_index : Int = 100
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        initData()
    }
    
    override func initUI() {
        bgView = UIView()
        bgView.bgHexColor = "#FFFFFF"
        bgView.cornerRadius = 10.k_h
        
        line = UIView()
        line.bgHexColor = "#000000"
        
        name = UILabel()
        name.text = "Effect"
        name.font = font(16,.bold)
        name.textHexColor = "#000000"
        name.textAlignment = .center
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: UITableViewCell.ID())
        
        view.addSubview(bgView)
        bgView.addSubview(name)
        bgView.addSubview(line)
        bgView.addSubview(tableView)
        
        bgView.snp.makeConstraints { make in
            make.height.equalTo(k_h(295))
            make.left.right.equalToSuperview().inset(k_w(30))
            make.centerY.equalToSuperview()
        }
        
        name.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10)
            make.height.equalTo(30)
            make.top.equalToSuperview()
        }
        
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10)
            make.height.equalTo(1)
            make.top.equalTo(name.snp.bottom)
        }
        
        tableView.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom)
            make.left.right.bottom.equalToSuperview().inset(0)
        }
    }
    
    override func initData() {
        let data = [[-1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 2, 2, 1, 0, 0, 0, 0, -1, -1, -1, -1],
                    [8, 8, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 3, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0],
                    [5, 6, 7, 5, 3, 1, 3, 4, 4, 6, 4, 2, 0, -1, -3, 0, 1, 2, 3, 4, 5, 6, 7, 5, 4, 6, 7, 6, 5, 4, 4],
                    [6, 5, 5, 4, 3, 2, 1, 1, 0, 0, 0, 1, 1, 0, -1, 0, 0, 0, 0, 1, 2, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3],
                    [-4, -4, -4, -4, -4, -4, -3, -2, -2, -2, -1, 2, 3, 4, 4, 6, 7, 9, 9, 7, 5, 4, 2, 1, 0, -1, -2, -2, -3, -3,-3],
                    [7, 6, 5, 4, 5, 6, 7, 8, 9, 9, 8, 6, 4, 2, 0, 0, -1, -2, -3, -4, -2, -1, 0, 1, 2, 3, 2, 1, 0, -1, -2]]
        array = ["POP","Dance","Rock","Classic","Vocal","Bass"].enumerated().map{CellMode(name: $0.element, data: data[$0.offset])}
        tableView.reloadData()
    }
    
}
//MARK: - UITableViewDataSource
extension EffectController:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: UITableViewCell.ID())
        cell?.contentView.removeAllSubView()
        cell?.selectionStyle = .none
        
        let isselect = indexPath.row == select_index
        let mode = array[indexPath.row]
        cell?.contentView.bgHexColor = isselect ? HEX_RED : HEX_WHITE
        
        let lbl = UILabel()
        lbl.text = mode.name
        lbl.font = font(16,isselect ? .bold : .regular)
        lbl.textHexColor = HEX_BLACK
        lbl.textAlignment = .center
        
        let line = UIView()
        line.bgHexColor = HEX_BLACK
        
        cell?.contentView.addSubview(lbl)
        cell?.contentView.addSubview(line)
        
        lbl.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10)
            make.top.bottom.equalToSuperview()
            make.height.equalTo(44)
        }
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10)
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
        return cell ?? UITableViewCell()
    }
}
//MARK: - UITableViewDelegate
extension EffectController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let mode = array[indexPath.row]
        if select_index == indexPath.row{return}
        select_index = indexPath.row
        tableView.reloadData()
        block?(mode.data, select_index)
        hidden()
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}

